package com.success.application.shop.holder;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.transition.Transition;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.ViewTarget;
import com.success.application.base.BaseHolder;
import com.success.application.myapplication.R;
import com.success.application.shop.bean.Love;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import butterknife.Bind;

import static com.success.application.myapplication.R.array.url;

/**
 * Created by ChrisWong on 2017/1/17.
 */

public class LoveHolder extends BaseHolder<Love> {
    private Love mlove;
    //背景
    @Bind(R.id.wish_row_image)
    RelativeLayout wish_row_image;
    //表白主题
    @Bind(R.id.iv_order_reserve)
    TextView iv_order_reserve;
    public LoveHolder(View itemView) {
        super(itemView);
    }

    /**
     * 根据图片的url路径获得Bitmap对象
     * @param url
     * @return
     */
    private Bitmap returnBitmap(String url) {
        URL fileUrl = null;
        Bitmap bitmap = null;

        try {
            fileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            HttpURLConnection conn = (HttpURLConnection) fileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;

    }


    @Override
    public void bindItem(Love love) {
        mlove = love;
        if(mlove.getXyjy_jytb()!=null&&mlove.getXyjy_jytb()!=""){
            Glide.with(wish_row_image.getContext()).load(mlove.getXyjy_jytb())
                    .into(new ViewTarget<View, GlideDrawable>(wish_row_image) {
                        //括号里为需要加载的控件
                        @Override
                        public void onResourceReady(GlideDrawable resource,
                                                    GlideAnimation<? super GlideDrawable> glideAnimation) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                this.view.setBackground(resource.getCurrent());
                            }
                        }
                    });
        }else{
//            wish_row_image.setBackgroundDrawable(R.mipmap.ic_launcher);
        }
        iv_order_reserve.setText(mlove.getXyjy_ztc());
        //设置文本信息
    }
    @Override
    public void onClick(View v) {
//        TestDetailActivity.show(v.getContext(), mConsult);
    }

    @Override
    public boolean onLongClick(View v) {
//        Toast.makeText(v.getContext(),
//                mConsult.getNc()+ "长按事件触发！", Toast.LENGTH_SHORT)
//                .show();
        return true;
    }
}
